package com.jiankang.controller;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jiankang.entity.AdviceForUser;
import com.jiankang.entity.HealthFile;
import com.jiankang.entity.Healthcare;
import com.jiankang.entity.Makeappointment;
import com.jiankang.entity.User;
import com.jiankang.entity.Usercomment;
import com.jiankang.service.AdviceForUserService;
import com.jiankang.service.HealthFileService;
import com.jiankang.service.HealthcareService;
import com.jiankang.service.UserService;
import com.jiankang.service.UsercommentService;
import com.jiankang.util.Md5;
import com.jiankang.util.Page;
@Controller
@RequestMapping("/user")
public class UserController {
	@Resource
	private UserService  userService ;
	//登陆
	@RequestMapping("/backstagelogin")
	public String backstagelogin(User user,HttpServletRequest request)
	{
		String str=Md5.md5s(user.getPassword()); 
		user.setPassword(str);
		User reUser=userService.login(user);
		if(reUser==null)
		{
			request.setAttribute("errorMsg", "账号或密码错误!");
			return "login";
		}else
		{
			user.setTime(new Date());
			userService.update(user);
			HttpSession session=request.getSession();
			session.setAttribute("currentUser", reUser);
			
			return "main";
		}
	} 
	//退出
	@RequestMapping("/toLogin")
	public String execute(HttpSession session){
		session.invalidate();
		return "login";
	}
	@RequestMapping(value = "/listusers", method = RequestMethod.GET)
	public String showForm(ModelMap model, HttpServletRequest req) {
		String pageNow = req.getParameter("pageNow");
		Page page = null;
		List<User> users = null;
		int totalCount = (int) userService.getAllUserCount();
		if (pageNow != null) {
			page = new Page(totalCount, Integer.parseInt(pageNow));
			users = userService.getUsers(page);
		} else {
			page = new Page(totalCount, 1);
			users = userService.getUsers(page);
		}
		model.addAttribute("page", page);
		model.addAttribute("users", users);
		return "right";
	}
	//删除
	@RequestMapping(value = "/delete/{id}")
	public String delete(@PathVariable("id") int id, ModelMap model,HttpServletRequest req) {
		userService.deleteById(id);
		return this.showForm(model, req);
	}
	//工作台
	@Resource
	private HealthFileService HealthFileService;
	@Resource
	private AdviceForUserService adviceForUserService;
	@Resource
	private HealthcareService healthcareService;
	@Resource
	private UsercommentService usercommentService;
	@RequestMapping("/statistics")
	public String statistics(ModelMap model){
		List<User> user=userService.getAllUser();
		List<HealthFile> healthfile=HealthFileService.searchfile();
		List<AdviceForUser> adviceforUser=adviceForUserService.questList();
		List<AdviceForUser> adviceforUserask=adviceForUserService.askList();
		List<Healthcare> selectthcare=healthcareService.selectthcare();
		List<Makeappointment> makeappointment=healthcareService.selectmakeappointment();
		List<Usercomment> usercomment=usercommentService.listcomment();
		model.addAttribute("user", user.size());
		model.addAttribute("healthfile", healthfile.size());
		model.addAttribute("adviceforUser", adviceforUser.size());
		model.addAttribute("adviceforUserask", adviceforUserask.size());
		model.addAttribute("selectthcare", selectthcare.size());
		model.addAttribute("makeappointment", makeappointment.size());
		model.addAttribute("usercomment", usercomment);
		return "default";
	}
}
